package com.fl.nj.service.impl;

import com.fl.nj.entity.ServiceType;
import com.fl.nj.mapper.ServiceTypeMapper;
import com.fl.nj.service.ServiceTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
import java.util.Map;

/**
 * 服务类型Service实现类
 * 实现服务类型的业务操作
 */
@Service
public class ServiceTypeServiceImpl implements ServiceTypeService {
    
    @Autowired
    private ServiceTypeMapper serviceTypeMapper;
    
    @Override
    public boolean save(ServiceType serviceType) {
        // 检查服务类型名称是否已存在
        ServiceType existing = serviceTypeMapper.getByTypeName(serviceType.getTypeName());
        if (existing != null) {
            return false;
        }
        return serviceTypeMapper.save(serviceType) > 0;
    }
    
    @Override
    public boolean update(ServiceType serviceType) {
        // 检查服务类型是否存在
        ServiceType existing = serviceTypeMapper.getById(serviceType.getId());
        if (existing == null) {
            return false;
        }
        
        // 检查服务类型名称是否已被其他记录使用
        ServiceType typeByName = serviceTypeMapper.getByTypeName(serviceType.getTypeName());
        if (typeByName != null && !typeByName.getId().equals(serviceType.getId())) {
            return false;
        }
        
        return serviceTypeMapper.update(serviceType) > 0;
    }
    
    @Override
    public boolean delete(Long id) {
        // 检查服务类型是否存在
        ServiceType existing = serviceTypeMapper.getById(id);
        if (existing == null) {
            return false;
        }
        
        return serviceTypeMapper.delete(id) > 0;
    }
    
    @Override
    public ServiceType getById(Long id) {
        return serviceTypeMapper.getById(id);
    }
    
    @Override
    public ServiceType getByTypeName(String typeName) {
        return serviceTypeMapper.getByTypeName(typeName);
    }
    
    @Override
    public List<ServiceType> getAll() {
        return serviceTypeMapper.getAll();
    }
    
    @Override
    public List<ServiceType> getPageList(Map<String, Object> params) {
        // 计算分页起始位置
        Integer page = (Integer) params.get("page");
        Integer pageSize = (Integer) params.get("pageSize");
        if (page != null && pageSize != null) {
            params.put("start", (page - 1) * pageSize);
        }
        return serviceTypeMapper.getPageList(params);
    }
    
    @Override
    public int getTotal(Map<String, Object> params) {
        return serviceTypeMapper.getTotal(params);
    }
}